package org.jeecg.modules.qingxjXiaoxtx.job;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.leave.entity.AgLeave;
import org.jeecg.modules.leave.service.IAgLeaveService;
import org.jeecg.modules.qingxjXiaoxtx.entity.AgQingxjXiaoxtx;
import org.jeecg.modules.qingxjXiaoxtx.entity.JsonParamPO;
import org.jeecg.modules.qingxjXiaoxtx.service.IAgQingxjXiaoxtxService;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.service.ISysUserService;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.client.RestTemplate;

import java.util.Date;
import java.util.List;

// 定时扫描请假表，超过请假时间的写入请销假告警消息表
@Slf4j
public class ScanYujwg implements Job {
    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private IAgQingxjXiaoxtxService agQingxjXiaoxtxService;

    @Autowired
    private IAgLeaveService agLeaveService;

    @Autowired
    private ISysUserService sysUserService;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        log.error(String.format("扫描请销假。余假未归定时任务执行了，时间:" + DateUtils.getTimestamp()));
        long t1 = System.currentTimeMillis();
        //        lt：less than 小于
//        le：less than or equal to 小于等于
//        eq：equal to 等于
//        ne：not equal to 不等于
//        ge：greater than or equal to 大于等于
//        gt：greater than 大于
        try {
            //余假未归
            List<AgLeave> list = agLeaveService.list(new QueryWrapper<AgLeave>()
                    .isNull("warning_flag")
                    .isNull("xiaoj_time")
                    //请假结束日期  小于  今天日期 是余假未归
                    .le("leave_endTime", DateUtils.formatDateTime()));
            for (AgLeave agLeave : list) {
                AgQingxjXiaoxtx agQingxjXiaoxtx = new AgQingxjXiaoxtx();
                //写入请销假消息提醒表
                agQingxjXiaoxtx.setCreateTime(new Date());
                agQingxjXiaoxtx.setGuanlr(agLeave.getApplicant());
                agQingxjXiaoxtx.setXiaoxlx("2");
                //获取请假人领导(审批人)
                String auditing = agLeave.getFirstAudit();
                //用申请人查询user表的username，获取请假人个人信息，并写入写入请销假消息提醒表
                String applicant = agLeave.getApplicant();
                SysUser sysUser = sysUserService.getOne(new QueryWrapper<SysUser>().eq("username", applicant));
                String realname = sysUser.getRealname();
                String email = sysUser.getEmail();
                String phone = sysUser.getPhone();
                agQingxjXiaoxtx.setXiaoxnr("已超过请假时间，需要电话联系请假人"+"\n"+"真实姓名:"+realname+"\n"+"电子邮件:"+email+"\n"+"电话:"+phone);
                //修改请假表的告警标志  0不告警  1已经告警,只警告一次
                String warningFlag = agLeave.getWarningFlag();
                if (warningFlag==null||!warningFlag.equals("1")){
                    //发送警告提醒  参数：请假人领导(审批人)，发送的消息，发送的链接
                    JsonParamPO jsonParamPO = new JsonParamPO();
                    jsonParamPO.setUserName(auditing);
                    jsonParamPO.setMessageContent(agQingxjXiaoxtx.getXiaoxnr());
                    jsonParamPO.setViewURL("http://26.29.23.94/anguan/%23/xiaoj");
                    String jsonStr = JSON.toJSONString(jsonParamPO);
                    tixing(jsonStr);
                    agLeave.setWarningFlag("1");
                    agLeaveService.updateById(agLeave);
                }
                agQingxjXiaoxtxService.save(agQingxjXiaoxtx);
            }
        } catch (Exception error) {
            log.error(error.getMessage());
        }

//        try {
//            //准备销假
//            List<AgLeave> list = agLeaveService.list(new QueryWrapper<AgLeave>()
//                    .isNull("warning_flag")
//                    .isNull("xiaoj_time")
//                    //请假结束日期  大于  今天日期 是准备销假
//                    .gt("leave_endTime", DateUtils.formatDateTime()));
//            for (AgLeave agLeave : list) {
//                AgQingxjXiaoxtx agQingxjXiaoxtx = new AgQingxjXiaoxtx();
//                //写入请销假消息提醒表
//                agQingxjXiaoxtx.setCreateTime(new Date());
//                agQingxjXiaoxtx.setGuanlr(agLeave.getApplicant());
//                agQingxjXiaoxtx.setXiaoxlx("2");
//                //获取请假人领导(审批人)
//                String auditing = agLeave.getFirstAudit();
//                //用申请人查询user表的username，获取请假人个人信息，并写入写入请销假消息提醒表
//                String applicant = agLeave.getApplicant();
//                SysUser sysUser = sysUserService.getOne(new QueryWrapper<SysUser>().eq("username", applicant));
//                String realname = sysUser.getRealname();
//                String email = sysUser.getEmail();
//                String phone = sysUser.getPhone();
//                agQingxjXiaoxtx.setXiaoxnr("准备销假，需要电话联系请假人" + "\n" + "真实姓名:" + realname + "\n" + "电子邮件:" + email + "\n" + "电话:" + phone);
//                //提前24小时发送
//                Date date = new Date();
//                Date leaveEndtime = agLeave.getLeaveEndtime();
//                long hours =(date.getTime()-leaveEndtime.getTime())/(60*60*1000);
//                //System.out.println(hours);
//                //修改请假表的告警标志  0不告警  1已经告警,只警告一次
//                String warningFlag = agLeave.getWarningFlag();
//                if ((warningFlag == null || !warningFlag.equals("0")) & hours <= 24) {
//                    //发送警告提醒  参数：请假人领导(审批人)，发送的消息，发送的链接
//                    JsonParamPO jsonParamPO = new JsonParamPO();
//                    jsonParamPO.setUserName(auditing);
//                    jsonParamPO.setMessageContent(agQingxjXiaoxtx.getXiaoxnr());
//                    jsonParamPO.setViewURL("http://26.29.23.94/anguan/%23/xiaoj");
//                    String jsonStr = JSON.toJSONString(jsonParamPO);
//                    tixing(jsonStr);
//                    agLeave.setWarningFlag("0");
//                    agLeaveService.updateById(agLeave);
//                }
//                agQingxjXiaoxtxService.save(agQingxjXiaoxtx);
//            }
//        } catch (Exception error) {
//            log.error(error.getMessage());
//        }
        long t2 = System.currentTimeMillis();
        log.info(" 扫描请销假，任务执行结束了，执行时间:" + (t2-t1));
    }

    public String tixing(String jsonStr) {
//        Map map = new HashMap();
//        map.put("userName",jsonParamPO.getUsername());
//        map.put("messageContent",jsonParamPO.getMessageContent());
//        map.put("viewURL",jsonParamPO.getViewURL());
        //"http://26.29.23.62:8080/api/jsonws/FeedBack-portlet.feedback/addNotification?userName={userName}&messageContent={messageContent}&viewURL={viewURL}"
        String result = restTemplate.getForObject(
                "http://26.29.23.62:8080/api/jsonws/FeedBack-portlet.feedback/addNotification?jsonParam=" + jsonStr,
                String.class);
        return result;
    }

}
